const fs = require('fs');
const path = require('path');

// // 读取文件的流
// // 两个文件
// const fileName1 = path.resolve(__dirname, 'data.txt');
// const fileName2 = path.resolve(__dirname, 'data-bak.txt');

// // 设置两个 stream 对象
// const readStream = fs.createReadStream(fileName1);
// const writeStream = fs.createWriteStream(fileName2);

// readStream.pipe(writeStream);
// // 监听读取流
// readStream.on('data', chunk => {
//   console.log(chunk.toString());
// })
// // 读取完成后
// readStream.on('end', () => {
//   console.log('done');
// });


// http get 获取文件流
const http = require('http');
const fileName = path.resolve(__dirname, 'data.txt');
const server = http.createServer((req, res) => {
  if (req.method === 'GET') {
    const readStream = fs.createReadStream(fileName);
    readStream.pipe(res);
  }
});

server.listen(8000);
